interface TypeMapping {
  [key: string]: {
    jsType: string
    defaultValue: any
  }
}

const JAVA_TO_JS: TypeMapping = {
  String: { jsType: 'string', defaultValue: '""' },
  Long: { jsType: 'number', defaultValue: 0 },
  Date: { jsType: 'string', defaultValue: 'null' },
  List: { jsType: 'Array', defaultValue: '[]' },
}

export function convertJavaToJS(javaClass: string): string {
  const lines = javaClass.split('\n')
  let output = 'const result = {\n'

  lines.forEach((line) => {
    const trimmed = line.trim()

    // 解析字段注解
    const fieldAnnotationMatch = trimmed.match(/@TableField\("(\w+)"\)/)
    const fieldName = fieldAnnotationMatch?.[1] || ''

    // 提取注释内容（移除开头的*和空格）
    let theLabel: string = ''
    if (trimmed.startsWith('*')) {
      theLabel = trimmed.replace(/^\*\s?/, '').trim()
    } else {
      theLabel = fieldName
    }

    // 解析字段声明
    const fieldMatch = trimmed.match(/private\s+(\w+)\s+(\w+);/)
    if (fieldMatch && fieldName) {
      const [_, javaType, originalName] = fieldMatch
      const mapping = JAVA_TO_JS[javaType] || {
        jsType: 'any',
        defaultValue: 'null',
      }

      output += `  /**\n   * 原字段名: ${originalName}\n   */\n`
      output += ` '${fieldName}' :{ \n\n`
      output += `label:'${theLabel}',\n\n`
      output += `key:'${fieldName}',\n\n`
      output += `type:'${mapping.jsType}',\n\n`
      output += `defaultValue:${mapping.defaultValue},\n\n`
      output += `isList:${mapping.jsType === 'List'},\n\n`
      output += `}\n\n`
    }
  })

  output += '};\n\nreturn result;'
  return output
}

// 使用示例
const javaClass = `
public class JobPermit {
  @TableField("declare_date")
  private String declareDate;
  
  @TableField("dept_id")
  private Long deptId;
  
  @TableField("job_files")
  private List<String> jobFiles;
}
`

console.log(convertJavaToJS(javaClass))
